// 导入依赖的模块
yepjs.require("jquery");
yepjs.require("YBA.Common");
yepjs.require("YBA.Plugin.plugins");
yepjs.require("YBA.Plugin.highcharts");
yepjs.require("YBA.Data.Statistics");


// 安全审计 - 存储情况
yepjs.define("storage", {

    NS: "YBA.Logic.Statistics",

    field: function() {

        var Biz = {
            // 初始化事件
            init: function() {
                Biz.fn.getStorage(Biz.vars.data);
                for (var i in Biz.event) {
                    Biz.event[i]();
                }
            },
            //  公共变量
            vars: {
                // 模板列表
                tpl: (function(){
                    var tpl = {};
                    $("script[type='text/ysmart']").each(function(){
                        var t = $(this);
                        tpl[t.attr("data-name")] = t.html();
                    });
                    return tpl;
                })(),
                //暂定空参
                data:{}
            },
            // 页面元素
            el: {
                jstorageList:$("#j-storageList")
            },
            // 功能函数集合
            fn: {
                //获取当前客户的存储情况统计
                getStorage:function(data){
                    YBA.Data.Statistics.storage(data,function(json){
                        if(json.code == 200) {
                            Biz.el.jstorageList.html(YBA.Common.ysmart(Biz.vars.tpl.storageTpl, json.storageCase));
                        }else{
                            YBA.Plugin.msgtip({type: "i", msg: json.desc})
                        }
                    })
                },
                //存储总占比弹窗
                storageTrendsForm:function(json){
                    var popform = new YBA.Plugin.popform({
                        title:"数据源存储容量分布",
                        className:"ui-dialog-storage",
                        content:YBA.Common.ysmart(Biz.vars.tpl.storageTrendsTpl,json || {}),
                        width:864,
                        destroy:true,
                        onHide: function () {
                            // 隐藏弹窗时要执行的回调函数
                        },
                        onShow:function(){
                            $('#j-pieBasic').highcharts({
                                chart: {
                                    plotBackgroundColor: null,
                                    plotBorderWidth: null,
                                    plotShadow: false,
                                    type: 'pie'
                                },
                                credits:{
                                    text: '',
                                    href: ''
                                },
                                title: {
                                    text: '数据源存储容量分布'
                                },
                                tooltip: {
                                    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                                },
                                plotOptions: {
                                    pie: {
                                        allowPointSelect: true,
                                        cursor: 'pointer',
                                        dataLabels: {
                                            enabled: true,
                                            format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                                            style: {
                                                color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                                            }
                                        }
                                    }
                                },
                                series: [{
                                    name: "百分比",
                                    colorByPoint: true,
                                    data: json.terminalMemoryCase
                                }]
                            });
                        }
                    }).show();
                }

            },
            // 初始化时执行的所有事件
            event: {
                //查看终端存储容量分布
                storageTrends:function(){
                    Biz.el.jstorageList.delegate(".j-storage","click", function() {
                        var t = $(this), tr = t.closest("tr"),id = tr.attr("data-id");
                        var data = {
                            "terminalId":id
                        }
                        YBA.Data.Statistics.storageTrends(data,function(json){
                            if(json.code == 200) {
                                var caseData = {
                                    "terminalMemoryCase":[]
                                };
                                var totalCapacity = 100;
                                for(j=0;j<json.terminalMemoryCase.length;j++){
                                    var dataSave = {
                                        "name":"",
                                        "y":""
                                    }
                                    dataSave.name = json.terminalMemoryCase[j].name;
                                    dataSave.y = parseFloat(json.terminalMemoryCase[j].totalAccounting)*100;
                                    caseData.terminalMemoryCase[j] = dataSave;
                                    totalCapacity = totalCapacity - dataSave.y;
                                }
                                caseData.terminalMemoryCase[caseData.terminalMemoryCase.length] = {
                                    "name":"剩余可分配容量",
                                    "y":totalCapacity
                                }
                                Biz.fn.storageTrendsForm(caseData);
                            }else{
                                YBA.Plugin.msgtip({type: "i", msg: json.desc});
                            }
                        })
                    });
                }
            }
        };

        Biz.init();

        return Biz;
    }
});
